package com.akuhome.resource.service.check;

import com.akuhome.resource.DO.check.ClassroomCheck;
import com.akuhome.resource.DO.check.use.ClassroomUse;
import com.akuhome.resource.DO.check.use.ClassroomUse;
import com.akuhome.resource.dao.CheckDao;
import com.akuhome.resource.dto.use.ClassroomUseProject;
import com.akuhome.resource.dto.use.ClassroomUseProject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

@Service
public class ClassroomCheckService {
    @Autowired
    CheckDao checkDao;

    public ClassroomUseProject findClassroomUseProject(Integer cid){
        return checkDao.findClassroomUseProjectByProjectId(cid);
    }

    @Transactional(rollbackFor = {RuntimeException.class,Exception.class, SQLException.class})
    public void updateClassroomUsage(List<ClassroomUse> classroomUseList){
        //取第一个项目作为参照
        ClassroomUse simple = classroomUseList.get(0);
        List<ClassroomUse> oldList = checkDao.findClassroomUseByClassroomCheckId(simple.getClassroomCheckId());
        List<ClassroomUse> deleteList = new ArrayList<>(oldList);
        List<ClassroomUse> insertList = classroomUseList;
        deleteList.removeAll(insertList);
        insertList.removeAll(oldList);
        if(insertList.size()>0)
            checkDao.insertClassroomUseList(insertList);
        if(deleteList.size()>0)
            checkDao.deleteClassroomUseList(deleteList);
    }

    public Integer insertClassroomCheck(ClassroomCheck classroomCheck,String uid){
        checkDao.insertClassroomCheck(classroomCheck,uid);
        return classroomCheck.getId();
    }

    public void insertClassroomUse(List<ClassroomUse> classroomUse){
        checkDao.insertClassroomUseList(classroomUse);
    }
}
